package com.wswy.wzcx.widget

import android.content.Context
import android.graphics.Typeface
import android.support.v4.content.ContextCompat
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.facebook.common.util.UriUtil
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.wswy.wzcx.R
import com.wswy.wzcx.model.car.CarStatus
import com.wswy.wzcx.model.home.RPaths
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.module.base.onClick
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.car.remind.RemindActivity
import com.wswy.wzcx.ui.other.ClickUtils
import kotlinx.android.synthetic.main.query_result_info_head_layout.view.*

class ResultHeadLayout : RelativeLayout{

    companion object {
        private const val TAG = "ResultHeadLayout"
    }

    constructor(context: Context):this(context,null)

    constructor(context: Context,attribute: AttributeSet?):this(context,attribute,0)

    constructor(context: Context,attribute: AttributeSet?,defStyle:Int):super(context,attribute,defStyle){

        View.inflate(context, R.layout.query_result_info_head_layout,this)


        val clickListener = View.OnClickListener {
            if (ClickUtils.isFastClick()){
                return@OnClickListener
            }

            carInfo?.run {
                //AddCarInfoActivity.edit(context,this)

                modifyClickListener?.onClick(it)

                val id = if (carExtendInfo?.carTypeData?.model == null) StatisticsId.detail_addcar_series_null else StatisticsId.detail_addcar_series_have
                StatTools.sendClick(context,id)
            }
        }

        //车辆标题
        //tv_car_title.setOnClickListener(clickListener)
        //tv_car_title_hint.setOnClickListener(clickListener)

        ll_car_names.setOnClickListener(clickListener)

        //估值
        ll_center_info.getChildAt(0).onClick {
            carInfo?.run {
                RemindActivity.startValuation(context,this)


                val id = if (carExtendInfo?.valuation?.info == null) StatisticsId.detail_value_null else StatisticsId.detail_value_have
                StatTools.sendClick(context,id)

            }
        }

        //投保
        ll_center_info.getChildAt(1).onClick {
            carInfo?.run {
                RemindActivity.startInsurance(context,this)


                val id = if (carExtendInfo?.insuranceInfo == null) StatisticsId.detail_insurance_null else StatisticsId.detail_insurance_have
                StatTools.sendClick(context,id)

            }
        }

        //年检
        ll_center_info.getChildAt(2).onClick {
            carInfo?.run {
                RemindActivity.startInspection(context,this)


                val id = if (carExtendInfo?.inspection == null) StatisticsId.detail_inspection_null else StatisticsId.detail_inspection_have
                StatTools.sendClick(context,id)

            }
        }

        //驾照扣分
        ll_center_info.getChildAt(3).onClick {
            carInfo?.run {

                RouterUtils.startTarget(context,RouterUtils.TYPE_NATIVE,RPaths.jzcf)

                val id = if (driverLicense.score == null) StatisticsId.detail_licence_null else StatisticsId.detail_licence_have
                StatTools.sendClick(context,id)
            }
        }


    }


    private var carInfo:UserCarInfo?=null

    var modifyClickListener:View.OnClickListener?=null

    private var lastPressView:View?=null
    fun pressPoint(x:Float,y:Float){
        lastPressView?.isPressed = false

        ll_center_info.getChildAt((x / (width / 4)).toInt())?.run {

            lastPressView = this

            isPressed = true
        }
    }

    fun clickPoint(x:Float,y:Float){
        ll_center_info.getChildAt((x / (width / 4)).toInt())?.run {

            if (lastPressView !=null && lastPressView != this){
                lastPressView?.isPressed = false
            }

            performClick()
            isPressed = false
        }
    }

    fun resetPress(){
        lastPressView?.isPressed = false
        lastPressView = null
    }


    fun setCarInfo(carInfo: UserCarInfo?){
        this.carInfo = carInfo


        carInfo?.carExtendInfo?.carTypeData?.run {
            if (!TextUtils.isEmpty(model?.name)){
                tv_car_title.text =  "${brand?.name} ${series?.name}"
                tv_car_title_hint.text = "${model?.year} ${model?.name}"
            }else{
                tv_car_title.text =  "尚未设置车型"
                tv_car_title_hint.text = "请先设置车型"
            }
        }?:kotlin.run {
            tv_car_title.text =  "尚未设置车型"
            tv_car_title_hint.text = "请先设置车型"
        }


        if (!TextUtils.isEmpty(carInfo?.carExtendInfo?.carTypeData?.series?.logo)) {
            ImageRequestBuilder
                    .newBuilderWithSource(UriUtil.parseUriOrNull(carInfo?.carExtendInfo?.carTypeData?.series?.logo))
                    .build().run {

                        img_car.setImageRequest(this)
                    }

        }else{
            ImageRequestBuilder
                    .newBuilderWithResourceId(R.drawable.ic_avatar_car)
                    .build().run {
                        img_car.setImageRequest(this)
                    }
        }


        carInfo?.carExtendInfo?.run {

            //估值
            valuation?.value?.run {
                val sb = SpannableStringBuilder().append(this)

                SpannableString("万").run {
                    setSpan(StyleSpan(Typeface.NORMAL),0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    setSpan(RelativeSizeSpan(0.6f), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.white80)), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    sb.append(this)
                }
                tv_car_evaluate.text = sb
            }?: kotlin.run {
                tv_car_evaluate.text = "--"
            }

            //保险
            insuranceInfo?.run {


                val sb = SpannableStringBuilder().append(remainDay.toString())

                SpannableString("天").run {
                    setSpan(StyleSpan(Typeface.NORMAL),0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    setSpan(RelativeSizeSpan(0.6f), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.white80)), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    sb.append(this)
                }
                tv_next_insurance.text = sb

            } ?: kotlin.run {
                tv_next_insurance.text = "--"
            }


            //年检
            inspection?.run {

                tv_car_annual_inspect_hint.text = when(status){
                    CarStatus.NORMAL -> "年检办理"
                    CarStatus.PROMPT -> "年检到期"
                    CarStatus.OVERDUE -> "已逾期"
                    else -> "下次年检"
                }

                val sb = SpannableStringBuilder().append(remainDay.toString())

                SpannableString("天").run {
                    setSpan(StyleSpan(Typeface.NORMAL),0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    setSpan(RelativeSizeSpan(0.6f), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.white80)), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                    sb.append(this)
                }
                tv_car_annual_inspect.text = sb

            } ?: kotlin.run {
                tv_car_annual_inspect.text = "--"
                tv_car_annual_inspect_hint.text = "下次年检"
            }



        }?:kotlin.run {
            tv_car_evaluate.text = "--"
            tv_next_insurance.text = "--"
            tv_car_annual_inspect.text = "--"

            tv_car_annual_inspect_hint.text = "下次年检"
        }

        //驾驶证
        carInfo?.driverLicense?.score?.run {


            val sb = SpannableStringBuilder().append(this)

            SpannableString("分").run {
                setSpan(StyleSpan(Typeface.NORMAL),0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                setSpan(RelativeSizeSpan(0.6f), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.white80)), 0, length, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                sb.append(this)
            }

            tv_card_points.text = sb

        }?:kotlin.run {
            tv_card_points.text = "--"
        }

    }


}